import React from 'react';
import {
    Button, Space, Modal,
    Form, Checkbox, notification,
    Input, InputNumber, message,
} from 'antd';
import {
    ExclamationCircleOutlined
} from '@ant-design/icons';
import { connect } from 'umi';
import request from '@/utils/request';
import { WhseSelect } from '@/components/FormItemSelect';
import ItemSelect from '@/components/ItemSelect';
import PartSelect from '@/components/PartSelect';

const { confirm } = Modal;
const apiURL = "/api/services/app/RcvDtl/";
const apiURLPoRel = "/api/services/app/PORel/";
const currentControl = "RcvDtl";
class ViewPage extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            data: {},
        }
    }
    formRef: any = React.createRef();
    componentDidMount() {
        this.props.onRef(this)
        if (this.props.data) {
            this.setState({ data: this.props.data })
            this.formRef.current.setFieldsValue({ model: this.props.data });
        }
    }
    onFinish = (values: any) => {
        this.save(values.model)
    }
    save = (model: any) => {
        var act = 'New'
        var method = 'POST'
        if (model.id) {
            act = 'Modify'
        }
        this.setState({ loading: true });
        request(apiURL + act, {
            method,
            data: model
        }).then((response) => {
            if (response) {
                if (response.success) {
                    message.success('保存成功！');
                    this.props.refreshData(model.packSlip)
                }
                this.setState({ loading: false });
            }
        });
    }
    getObjPO = (po: number = 0, line: number = 0, relnum: number = 0) => {
        if (po === 0 || line === 0 || relnum === 0) {
            return;
        }
        var obj = this.formRef.current.getFieldsValue();
        request(apiURLPoRel + 'GetByNum', {
            method: 'GET',
            params: {
                num: po,
                line
            }
        }).then((response) => {
            if (response) {
                if (response.success) {
                    var result = response.result.data;
                    console.log(result);
                }
                else {
                    notification.error({
                        message: response.error.message,
                        description: response.error.details,
                    });
                }
                this.setState({ loading: false });
            }
        });
    }
    delObj = () => {
        const { data } = this.state;
        let _this = this
        confirm({
            title: '警告',
            icon: <ExclamationCircleOutlined />,
            content: '删除后将不可恢复，确定删除？',
            onOk() {
                _this.props.dispatch({
                    type: 'Abp/delete',
                    payload: { id: data.id },
                    control: currentControl
                }).then(() => {
                    _this.props.refreshData(data.packSlip)
                })
            },
        });

    }
    setCurrent = (item: any) => {
        let model = {
            poNum: item.poNum,
            poLine: item.poLine,
            poRelNum: item.poRelNum,
            partNum: item.partNum,
            partDescription: item.lineDesc,
            ium: item.ium,
            ourQty: item.relQty,
            vendorUnitCost: item.unitCost,
        }
        this.formRef.current.setFieldsValue({ model });
    }
    setCurrentJob = (item: any) => {
        let model = {
            jobNum: item.jobNum,
            assemblySeq: item.assemblySeq,
            jobSeq: item.mtlSeq,
            partNum: item.partNum,
            partDescription: item.partDescription,
            ium: item.ium,
            ourQty: item.prodQty,
        }
        this.formRef.current.setFieldsValue({ model });
    }
    setCurrentPart = (part?: any) => {
        if (part) {
            var model: any = {};
            model.partNum = part.partNum;
            model.partDescription = part.partDescription;
            model.ium = part.ium;
            this.formRef.current.setFieldsValue({ model });
        }
    }
    render() {
        const { loading } = this.state
        const layout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 16 },
        };
        const columnPoItem = [
            {
                title: '采购单号',
                dataIndex: 'poNum',
            },
            {
                title: '分录',
                dataIndex: 'poLine',
            },
            {
                title: '收货行',
                dataIndex: 'poRelNum',
                width: 90,
            },
            {
                title: '物料编码',
                dataIndex: 'partNum',
            },
            {
                title: '描述',
                dataIndex: 'lineDesc',
                ellipsis: true,
            },
            {
                title: '需求量',
                dataIndex: 'relQty',
                width: 90,
            },
            {
                title: '单位',
                dataIndex: 'ium',
            },
        ]
        const columnJobItem = [
            {
                title: '工单号',
                dataIndex: 'jobNum',
            },
            {
                title: '物料编码',
                dataIndex: 'partNum',
            },
            {
                title: '描述',
                dataIndex: 'partDescription',
            },
            {
                title: '生产数量',
                dataIndex: 'prodQty',
            },
            {
                title: '单位',
                dataIndex: 'ium',
            },
        ]
        return (
            <Form {...layout} ref={this.formRef} onFinish={this.onFinish} >
                <Form.Item name={['model', 'id']} hidden={true}>
                    <Input />
                </Form.Item>
                <Form.Item label="收货单号" name={['model', 'packSlip']} rules={[{ required: true }]}>
                    <Input disabled />
                </Form.Item>
                <Form.Item label="收货行" name={['model', 'packLine']}>
                    <InputNumber disabled />
                </Form.Item>
                <Form.Item name={['model', 'receiptType']} label="收货类型">
                    <Input disabled />
                </Form.Item>
                <Form.Item label="采购单" hidden={this.props.data.receiptType != 'MTL'}>
                    <Input.Group compact>
                        <Form.Item
                            noStyle
                            name={['model', 'poNum']}>
                            <Input style={{ width: 80 }} placeholder="采购单号" />
                        </Form.Item>
                        <Form.Item noStyle name={['model', 'poLine']}>
                            <InputNumber placeholder="分录" style={{ width: 80 }} />
                        </Form.Item>
                        <Form.Item noStyle name={['model', 'poRelNum']}>
                            <InputNumber placeholder="收货行" style={{ width: 80 }} />
                        </Form.Item>
                        <Form.Item noStyle>
                            <ItemSelect
                                width={780}
                                control='POHeader'
                                columns={columnPoItem}
                                text="选择"
                                title="采购单选择"
                                act="GetItems"
                                btntype="primary"
                                setCurrent={this.setCurrent} />
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Form.Item label="工单" hidden={this.props.data.receiptType != 'JOB'}>
                    <Input.Group compact>
                        <Form.Item
                            noStyle
                            name={['model', 'jobNum']}>
                            <Input style={{ width: 80 }} placeholder="工单号" />
                        </Form.Item>
                        <Form.Item noStyle name={['model', 'assemblySeq']}>
                            <InputNumber placeholder="半成品号" style={{ width: 80 }} />
                        </Form.Item>
                        <Form.Item noStyle name={['model', 'jobSeq']}>
                            <InputNumber placeholder="物料号/工序号" style={{ width: 80 }} />
                        </Form.Item>
                        <Form.Item noStyle>
                            <ItemSelect
                                width={780}
                                control='JobHead'
                                columns={columnJobItem}
                                text="选择"
                                title="工单物料"
                                act="GetList?completed=1"
                                btntype="primary"
                                setCurrent={this.setCurrentJob} />
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Form.Item label="物料编码">
                    <PartSelect
                        name={['model', 'partNum']}
                        noStyle
                        width={150}
                        required={true}
                        placeholder="物料编码"
                        setCurrent={this.setCurrentPart}
                    />
                </Form.Item>

                <Form.Item name={['model', 'partDescription']} label="物料描述" rules={[{ required: true }]}>
                    <Input disabled />
                </Form.Item>
                <Form.Item label="我方数量">
                    <Input.Group compact>
                        <Form.Item noStyle name={['model', 'ourQty']} rules={[{ required: true }]}>
                            <InputNumber />
                        </Form.Item>
                        <Form.Item noStyle name={['model', 'ium']}>
                            <Input style={{ width: 80 }} placeholder="单位" disabled />
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Form.Item name={['model', 'vendorUnitCost']} label="单位成本" hidden>
                    <InputNumber />
                </Form.Item>
                <Form.Item label="入料仓库" style={{ marginBottom: 0 }}>
                    <Input.Group compact>
                        <WhseSelect width={120} required={true} showSearch={false} name={['model', 'warehouseCode']} placeholder="入料仓库" />
                        <Form.Item name={['model', 'binNum']} rules={[{ required: true, message: "入料库位必填" }]}>
                            <Input style={{ width: 100 }} placeholder="库位" />
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 5 }}>
                    <Input.Group compact>
                        <Form.Item
                            name={['model', 'receivedComplete']}
                            valuePropName="checked">
                            <Checkbox disabled>已收完</Checkbox>
                        </Form.Item>
                        <Form.Item
                            name={['model', 'inspectionReq']}
                            valuePropName="checked">
                            <Checkbox>需要检验</Checkbox>
                        </Form.Item>
                    </Input.Group>
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 6 }}>
                    <Space>
                        <Button key="1" type="primary" htmlType="submit" loading={loading}>保存</Button>
                        <Button key="2" danger disabled={!this.state.data.id} onClick={this.delObj}>删除</Button>
                    </Space>
                </Form.Item>
            </Form>
        )
    }
}
export default connect(({ Abp, user }: API.IConnectState) => ({
    response: Abp.response,
    currentUser: user.currentUser,
}))(ViewPage);